using AiQiuQuan.Sport.Model.Models;
using AiQiuQuan.Sport.Core;
using AiQiuQuan.Sport.WebApi.Services;
using Microsoft.AspNetCore.Mvc;
using System.ComponentModel.DataAnnotations;
using Microsoft.AspNetCore.Authorization;

namespace AiQiuQuan.Sport.WebApi.Controllers
{
    /// <summary>
    /// 平台课程管理
    /// </summary>
    public class PlatformCourseController : PcApiController
    {
        private readonly ICourseService _courseService;
        private readonly ICourseOrderService _courseOrderService;

        /// <summary>
        /// 课程管理
        /// </summary>
        public PlatformCourseController(
            ICourseService courseService,
            ICourseOrderService courseOrderService)
        {
            _courseService = courseService;
            _courseOrderService = courseOrderService;
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id">课程ID</param>
        [HttpGet("get")]
        [AllowAnonymous]
        public async Task<UnaryResult<CourseDto>> GetAsync(Guid id)
        {
            var result = await _courseService.GetAsync(id);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程分页列表
        /// </summary>
        [HttpPost("pagelist")]
        public async Task<UnaryResult<PageResult<CourseDto>>> PageListAsync(CoursePageRequestDto request)
        {
            request.IsPlatform = true;
            var result = await _courseService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 新增
        /// </summary>
        [HttpPost("add")]
        public async Task<UnaryResult> AddAsync(CourseAddDto model)
        {
            var result = await _courseService.AddAsync(model);
            return result;
        }

        /// <summary>
        /// 课程相关状态、标记修改
        /// </summary>
        [HttpPost("update_flag")]
        public async Task<UnaryResult> UpdateCourseFlagAsync(CourseFlagUpdateDto request)
        {
            request.IsPlatform = true;
            var result = await _courseService.UpdateCourseFlagAsync(request);
            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        [HttpPost("update")]
        public async Task<UnaryResult> UpdateAsync(CourseUpdateDto model)
        {
            var result = await _courseService.UpdateAsync(model);
            return result;
        }

        /// <summary>
        /// 删除
        /// </summary>
        [HttpPost("delete")]
        public async Task<UnaryResult> DeleteAsync(Guid id)
        {
            var result = await _courseService.DeleteAsync(id);
            return result;
        }

        /// <summary>
        /// 获取课程学生列表
        /// </summary>
        /// <param name="courseId">课程ID</param>
        [HttpGet("list_student")]
        public async Task<UnaryResult<List<CourseStudentDto>>> QueryCourseStudentListAsync([Required] Guid courseId)
        {
            var result = await _courseService.QueryCourseStudentListAsync(courseId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程学生删除
        /// </summary>
        [HttpPost("delete_student")]
        public async Task<UnaryResult> DeleteStudentAsync(CourseStudentDelDto request)
        {
            request.IsPlatform = true;
            var result = await _courseService.DeleteStudentAsync(request);
            return result;
        }

        /// <summary>
        /// 获取课程教练列表
        /// </summary>
        /// <param name="courseId">课程ID</param>
        [HttpGet("list_coacher")]
        public async Task<UnaryResult<List<CourseCoacherDto>>> QueryCourseCoacherListAsync([Required] Guid courseId)
        {
            var result = await _courseService.QueryCourseCoacherListAsync(courseId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 获取教练课程列表
        /// </summary>
        /// <param name="coacherUserId">教练用户ID</param>
        [HttpGet("list_coacher_course")]
        public async Task<UnaryResult<List<CourseDto>>> GetCoacherCourseListAsync([Required] Guid coacherUserId)
        {
            var result = await _courseService.GetCoacherCourseListAsync(coacherUserId);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 删除课程教练
        /// </summary>
        [HttpPost("delete_coacher")]
        public async Task<UnaryResult> DeleteCourseCoacherAsync(CourseCoacherDelDto request)
        {
            var result = await _courseService.DeleteCourseCoacherAsync(request);
            return result;
        }

        /// <summary>
        /// 课程学生记录
        /// </summary>
        [HttpPost("list_student_record")]
        public async Task<UnaryResult<PageResult<CourseStuentRecordDto>>> QueryCourseStuentRecordListAsync(CourseStudentRecordPageRequestDto request)
        {
            request.IsPlatform = true;
            var result = await _courseService.QueryCourseStuentRecordListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程订单分页查询
        /// </summary>
        [HttpPost("pagelist_order")]
        public async Task<UnaryResult<PageResult<CourseOrderDto>>> PageListAsync(CourseOrderPageRequestDto request)
        {
            request.IsSelf = true;
            var result = await _courseOrderService.PageListAsync(request);
            return UnaryResult.Succeed(result);
        }

        /// <summary>
        /// 课程订单查询
        /// </summary>
        [HttpGet("get_order")]
        public async Task<UnaryResult<CourseOrderDto>> GetCourseOrderAsync(Guid orderId)
        {
            var result = await _courseOrderService.GetAsync(orderId);
            return UnaryResult.Succeed(result);
        }
    }
}
